﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UL360DataNET40;

namespace UL360Admin.Survey
{
    public partial class IntervalSettings : System.Web.UI.Page
    {
        public bool showSavedMessage = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.Form.Count <= 0)
                {
                    //New instance of intervalsettings
                    SurveyIntervalSetting interval = null;

                    //Get from database
                    using (var db = new UL360Entities())
                    {
                        interval = db.SurveyIntervalSettings.OrderByDescending(i => i.SurveyIntervalSettingId).DefaultIfEmpty(null).First();
                    }

                    if (interval == null)
                    {
                        //Set default in textboxes
                        SurveyIntervalSetting.Text = "2";
                        ReminderIntervalSetting.Text = "2";
                        LastAnswerSetting.Text = "2";
                    }
                    else
                    {
                        //Set values in textboxes
                        SurveyIntervalSetting.Text = interval.SendSurveyMailInterval.ToString();
                        ReminderIntervalSetting.Text = interval.SendReminderMailInterval.ToString();
                        LastAnswerSetting.Text = interval.LastAnswerInterval.ToString();
                    }
                }
            }
            catch (Exception exc)
            {
                string errorTicket = UL360SurveyCommon.LoggerCommon.LogNewEntry(
                    new UL360SurveyCommon.LoggerCommon.LogNewEntryDelegate(UL360Admin.Survey.Utils.Logger.LogNewEntry),
                    System.Reflection.MethodBase.GetCurrentMethod().Name, exc.ToString(), (int)UL360SurveyCommon.SurveyLoggingLevel.Error);

                WarningLabel.Text = string.Format("Ett fel uppstod, försök igen eller kontakta support med följande ID {0}", errorTicket);
            }
        }

        /// <summary>
        /// Save intervalsettings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SaveInterval_Click(object sender, EventArgs e)
        {
            try
            {
                //Declare int-variables for intervals
                int surveyInterval, reminderInterval, lastAnswerInteral;

                //Tryparsing textboxvalues to int-variables
                int.TryParse(SurveyIntervalSetting.Text, out surveyInterval);
                int.TryParse(ReminderIntervalSetting.Text, out reminderInterval);
                int.TryParse(LastAnswerSetting.Text, out lastAnswerInteral);

                //Database
                using (var db = new UL360Entities())
                {
                    //If there are any entries in the database
                    if (db.SurveyIntervalSettings.Count() > 0)
                    {
                        //Update entry
                        db.SurveyIntervalSettings.First().SendSurveyMailInterval = surveyInterval;
                        db.SurveyIntervalSettings.First().SendReminderMailInterval = reminderInterval;
                        db.SurveyIntervalSettings.First().LastAnswerInterval = lastAnswerInteral;
                    }
                    //If the table is empty
                    else
                    {
                        //Add new entry
                        db.SurveyIntervalSettings.Add(new SurveyIntervalSetting
                        {
                            SendSurveyMailInterval = surveyInterval,
                            SendReminderMailInterval = reminderInterval,
                            LastAnswerInterval = lastAnswerInteral
                        });
                    }

                    //Save to database
                    db.SaveChanges();
                }

                showSavedMessage = true;
            }
            catch (Exception exc)
            {
                string errorTicket = UL360SurveyCommon.LoggerCommon.LogNewEntry(
                    new UL360SurveyCommon.LoggerCommon.LogNewEntryDelegate(UL360Admin.Survey.Utils.Logger.LogNewEntry),
                    System.Reflection.MethodBase.GetCurrentMethod().Name, exc.ToString(), (int)UL360SurveyCommon.SurveyLoggingLevel.Error);

                WarningLabel.Text = string.Format("Ett fel uppstod, försök igen eller kontakta support med följande ID {0}", errorTicket);
            }
        }
    }
}